package com.itsxb.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itsxb.constants.GlobalExceptionCode;
import com.itsxb.mapper.CustomerCarMapper;
import com.itsxb.pojo.domain.CustomerCar;
import com.itsxb.pojo.vo.AddCarVo;
import com.itsxb.service.ICustomerCarService;
import com.itsxb.utils.AssertUtil;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 客户车辆 服务实现类
 * </p>
 *
 * @author 奇天书
 * @since 2023-12-21
 */
@Service
public class CustomerCarServiceImpl extends ServiceImpl<CustomerCarMapper, CustomerCar> implements ICustomerCarService {
    @Override
    public List<CustomerCar> loadCarList() {
        //
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        //1.参数校验
        //2.业务检验
        //2.1.当我查询车列表时，不需要进行任何业务校验
        //3.业务实现
        return super.list(new LambdaQueryWrapper<CustomerCar>().eq(CustomerCar::getCustomerId,loginIdAsLong));
    }

    @Override
    public void addCar(AddCarVo addCarVo) {
        //抽取公共的变量
        String carPlate = addCarVo.getCarPlate();
        String carType = addCarVo.getCarType();
        long customerId = StpUtil.getLoginIdAsLong();
        //1.参数校验
        //2.业务校验

        //2.1.判断添加car是否在乘客汽车表中存在
        CustomerCar car = super.getOne(new LambdaQueryWrapper<CustomerCar>().eq(CustomerCar::getCarPlate,carPlate));
        AssertUtil.isNull(car, GlobalExceptionCode.SERVICE_ERROR);
        //3.业务实现
        CustomerCar customerCar = new CustomerCar();
        customerCar.setCustomerId(customerId);
        customerCar.setCarPlate(carPlate);
        customerCar.setCarType(carType);
        customerCar.setCreateTime(new Date());
        super.save(customerCar);
    }
}
